package main

/*ort (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"sync"

	"github.com/gorilla/websocket"
)

type Client struct {
	UUID   string
	UserID string
	Socket *websocket.Conn
	Send   chan []byte
}

type ClientManager struct {
	sync.RWMutex
	Clients    map[string]*Client
	Register   chan *Client
	Unregister chan *Client
	Renewal    chan *Client
}

func main() {
	fmt.Println("aa")
}

func (c *Client) Write(close chan *Client) {
	for {
		select {
		case message, ok := <-c.Send:
			if !ok {
				return
			}
			c.Socket.WriteMessage(websocket.TextMessage, message)
		case <-c.Ctx.Done():
			return
		}
	}
}

func (manager *ClientManager) Start(ctx context.Context) {
	for {
		select {
		case conn := <-manager.Register:
			manager.Lock()
			manager.Clients[conn.UUID] = conn
			manager.Unlock()
			_, err := manager.affair.Register(ctx, &RegisterReq{
				UserID: conn.UserID,
				UUID:   conn.UUID,
				IP:     manager.IP,
			})
		case conn := <-manager.Unregister:
			_, err := manager.affair.Unregister(ctx, &UnregisterReq{
				UserID: conn.UserID,
				UUID:   conn.UUID,
			})
			conn.Socket.Close()
			close(conn.Send)
			delete(manager.Clients, conn.UUID)
		case conn := <-manager.Renewal:
			//...
			// Key renewal to redis
		}
	}
}

func (manager *ClientManager) Start(ctx context.Context) {
	for {
		select {
		case conn := <-manager.Register:
			manager.Lock()
			manager.Clients[conn.UUID] = conn
			manager.Unlock()
			_, err := manager.affair.Register(ctx, &RegisterReq{
				UserID: conn.UserID,
				UUID:   conn.UUID,
				IP:     manager.IP,
			})
		case conn := <-manager.Unregister:
			_, err := manager.affair.Unregister(ctx, &UnregisterReq{
				UserID: conn.UserID,
				UUID:   conn.UUID,
			})
			conn.Socket.Close()
			close(conn.Send)
			delete(manager.Clients, conn.UUID)
		case conn := <-manager.Renewal:
			//...
			// Key renewal to redis
		}
	}
}

func (manager *ClientManager) Write(message *Message) error {
	manager.RLock()
	client, ok := manager.Clients[message.Recipient]
	manager.RUnlock()
	if !ok {
		return errors.New("client miss [" + message.Recipient + "]")
	}
	return client.SendOut(message)
}

func (c *Client) SendOut(message *Message) error {
	content, err := json.Marshal(message.Content)
	if err != nil {
		return err
	}
	c.Send <- content
	return nil
}
*/
